<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="80px"
    >
      <el-form-item label="" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="productGranularity">
        <el-input
          v-model="queryParams.productGranularity"
          placeholder="请输入颗粒度对象"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="allocationAtatus">
        <!-- <el-input
          v-model="queryParams.brand"
          placeholder="是否可分配"
          clearable
          @keyup.enter.native="handleQuery"
        /> -->
        <el-select
          v-model="queryParams.allocationAtatus"
          placeholder="是否可分配"
          clearable
        >
          <el-option
            v-for="dict in dict.type.sys_order_allocation"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="" prop="purchaseOrderNumber">
        <el-input
          v-model="queryParams.purchaseOrderNumber"
          placeholder="请输入采购单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="brand">
        <el-input
          v-model="queryParams.brand"
          placeholder="请输入品牌"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="已入仓" prop="warehousedQty">
        <el-input
          v-model="queryParams.warehousedQty"
          placeholder="请输入已入仓"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="已分配" prop="allocatedQty">
        <el-input
          v-model="queryParams.allocatedQty"
          placeholder="请输入已分配"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="待分配" prop="unassignedQty">
        <el-input
          v-model="queryParams.unassignedQty"
          placeholder="请输入待分配"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="下单数量" prop="orderQty">
        <el-input
          v-model="queryParams.orderQty"
          placeholder="请输入下单数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商" prop="supplierName">
        <el-input
          v-model="queryParams.supplierName"
          placeholder="请输入供应商"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="采购金额" prop="purchaseAmount">
        <el-input
          v-model="queryParams.purchaseAmount"
          placeholder="请输入采购金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="在途数量" prop="inTransitQuantity">
        <el-input
          v-model="queryParams.inTransitQuantity"
          placeholder="请输入在途数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="差异数量" prop="differenceCount">
        <el-input
          v-model="queryParams.differenceCount"
          placeholder="请输入差异数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="" prop="sku">
        <el-input
          v-model="queryParams.sku"
          placeholder="请输入sku"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="skc">
        <el-input
          v-model="queryParams.skc"
          placeholder="请输入skc"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="spu">
        <el-input
          v-model="queryParams.spu"
          placeholder="请输入spu"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="item">
        <el-input
          v-model="queryParams.item"
          placeholder="请输入item"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="isNew">
        <el-select
          v-model="queryParams.isNew"
          placeholder="请输入是否新品"
          filterable
        >
          <el-option
            v-for="dict in dict.type.sys_order_isnew"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="" prop="orderStatus">
        <!-- <el-input
          v-model="queryParams.isNew"
          placeholder="请输入是否新品"
          clearable
          @keyup.enter.native="handleQuery"
        /> -->
        <el-select
          v-model="queryParams.orderStatus"
          placeholder="订单状态"
          filterable
        >
          <el-option
            v-for="dict in dict.type.sys_order_orderstatus"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="合同号" prop="contractNumber">
        <el-input
          v-model="queryParams.contractNumber"
          placeholder="请输入合同号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['productAllocation:triggerItem:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['productAllocation:triggerItem:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['productAllocation:triggerItem:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['productAllocation:triggerItem:export']"
          >导出</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="primary" plain size="mini" @click="handlebyids"
          >触发SKC分货</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain size="mini" @click="handleSpubyids"
          >触发Spu分货</el-button
        >
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="primary" plain size="mini" @click="handleItembyids"
          >触发Item分货</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="triggerItemList"
      @selection-change="handleSelectionChange"
      :height="650"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column
        label="颗粒度对象"
        align="center"
        prop="productGranularity"
      />
      <el-table-column label="订单状态" align="center" prop="orderStatus" />
      <el-table-column
        label="采购单号"
        align="center"
        prop="purchaseOrderNumber"
      />
      <el-table-column label="订单类型" align="center" prop="orderType" />
      <el-table-column label="品牌" align="center" prop="brand" />
      <el-table-column label="已入仓" align="center" prop="warehousedQty" />
      <el-table-column label="已分配" align="center" prop="allocatedQty">
        <template slot-scope="scope">
          <span
            slot="reference"
            style="color: #409eff; cursor: pointer"
            size="mini"
            @click="getBiCommentSecondDwdList(scope.row)"
            >{{ scope.row.allocatedQty }}</span
          >
        </template>
      </el-table-column>
      <el-table-column label="待分配" align="center" prop="unassignedQty" />
      <el-table-column label="留仓数量" align="center" prop="abnormalCount">
        <template slot-scope="scope">
          <span
            slot="reference"
            style="color: #409eff; cursor: pointer"
            size="mini"
            @click="handleForAlnomal(scope.row)"
            >{{ scope.row.abnormalCount }}</span
          >
        </template>
      </el-table-column>
      <el-table-column label="下单数量" align="center" prop="orderQty" />
      <el-table-column label="供应商" align="center" prop="supplierName" />
      <el-table-column label="采购金额" align="center" prop="purchaseAmount" />
      <el-table-column
        label="在途数量"
        align="center"
        prop="inTransitQuantity"
      />
      <el-table-column label="差异数量" align="center" prop="differenceCount">
        <template slot-scope="scope">
          {{ scope.row.differenceCount }}
          <el-badge
            v-if="scope.row.differenceCount < 0"
            :value="'超'"
            class="item"
          ></el-badge>
          <el-badge
            v-if="scope.row.differenceCount > 0"
            :value="'缺'"
            type="success"
            class="item"
          ></el-badge>
        </template>
      </el-table-column>
      <el-table-column label="sku" align="center" prop="sku" />
      <el-table-column label="skc" align="center" prop="skc" />
      <el-table-column label="spu" align="center" prop="spu" />
      <el-table-column label="item" align="center" prop="item">
        <template slot-scope="scope">
          <span
            slot="reference"
            style="color: #409eff; cursor: pointer"
            size="mini"
            @click="getBiCommentSecondDwdList(scope.row)"
            >{{ scope.row.item }}</span
          >
        </template>
      </el-table-column>
      <el-table-column label="是否新品" align="center" prop="isNew">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_order_isnew"
            :value="scope.row.isNew"
          />
        </template>
      </el-table-column>
      <el-table-column label="入仓比例" align="center" prop="currentRatioValue">
        <template slot-scope="scope"
          >{{ scope.row.currentRatioValue }}%</template
        ></el-table-column
      >
      <el-table-column
        label="是否可分配"
        align="center"
        prop="allocationAtatus"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_order_allocation"
            :value="scope.row.allocationAtatus"
          />
        </template>
      </el-table-column>
      <el-table-column label="合同号" align="center" prop="contractNumber" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['productAllocation:triggerItem:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['productAllocation:triggerItem:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改采购分配触发ITEM对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="颗粒度对象" prop="productGranularity">
          <el-input
            v-model="form.productGranularity"
            placeholder="请输入颗粒度对象"
          />
        </el-form-item>
        <el-form-item label="采购单号" prop="purchaseOrderNumber">
          <el-input
            v-model="form.purchaseOrderNumber"
            placeholder="请输入采购单号"
          />
        </el-form-item>
        <el-form-item label="是否可分配" prop="allocationAtatus">
          <!-- <el-input v-model="form.allocationAtatus" placeholder="是否可分配" /> -->
          <el-select
            v-model="form.allocationAtatus"
            placeholder="是否可分配"
            clearable
          >
            <el-option
              v-for="dict in dict.type.sys_order_allocation"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="品牌" prop="brand">
          <el-input v-model="form.brand" placeholder="请输入品牌" />
        </el-form-item>
        <el-form-item label="已入仓" prop="warehousedQty">
          <el-input v-model="form.warehousedQty" placeholder="请输入已入仓" />
        </el-form-item>
        <el-form-item label="已分配" prop="allocatedQty">
          <el-input v-model="form.allocatedQty" placeholder="请输入已分配" />
        </el-form-item>
        <el-form-item label="待分配" prop="unassignedQty">
          <el-input v-model="form.unassignedQty" placeholder="请输入待分配" />
        </el-form-item>
        <el-form-item label="下单数量" prop="orderQty">
          <el-input v-model="form.orderQty" placeholder="请输入下单数量" />
        </el-form-item>
        <el-form-item label="供应商" prop="supplierName">
          <el-input v-model="form.supplierName" placeholder="请输入供应商" />
        </el-form-item>
        <el-form-item label="采购金额" prop="purchaseAmount">
          <el-input
            v-model="form.purchaseAmount"
            placeholder="请输入采购金额"
          />
        </el-form-item>
        <el-form-item label="在途数量" prop="inTransitQuantity">
          <el-input
            v-model="form.inTransitQuantity"
            placeholder="请输入在途数量"
          />
        </el-form-item>
        <el-form-item label="差异数量" prop="differenceCount">
          <el-input
            v-model="form.differenceCount"
            placeholder="请输入差异数量"
          />
        </el-form-item>
        <el-form-item label="sku" prop="sku">
          <el-input v-model="form.sku" placeholder="请输入sku" />
        </el-form-item>
        <el-form-item label="skc" prop="skc">
          <el-input v-model="form.skc" placeholder="请输入skc" />
        </el-form-item>
        <el-form-item label="spu" prop="spu">
          <el-input v-model="form.spu" placeholder="请输入spu" />
        </el-form-item>
        <el-form-item label="item" prop="item">
          <el-input v-model="form.item" placeholder="请输入item" />
        </el-form-item>
        <el-form-item label="是否新品" prop="isNew">
          <el-select
            v-model="form.isNew"
            placeholder="请输入是否新品"
            filterable
          >
            <el-option
              v-for="dict in dict.type.sys_order_isnew"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="合同号" prop="contractNumber">
          <el-input v-model="form.contractNumber" placeholder="请输入合同号" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog
      :visible.sync="openInfo"
      width="1500px"
      append-to-body
      v-if="openInfo"
    >
      <div class="el-dialog-div">
        <component :is="'trigger'" :rowitem="row"></component>
      </div>
    </el-dialog>
    <el-dialog
      :visible.sync="openForAlnomal"
      width="1500px"
      append-to-body
      v-if="openForAlnomal"
    >
      <div class="el-dialog-div">
        <component :is="'alnomalRestock'" :rowAlnomal="row"></component>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listTriggerSkc,
  getTriggerSkc,
  delTriggerSkc,
  addTriggerSkc,
  updateTriggerSkc,
  byids,
  previewAllocationForSkc,
  AllocationLogPreview,
  triggerSpubyids,
  triggerItembyids,
} from "@/api/productAllocation/triggerSkc";
import {
  listTriggerItem,
  getTriggerItem,
  delTriggerItem,
  addTriggerItem,
  updateTriggerItem,
} from "@/api/productAllocation/triggerItem";
import trigger from "@/views/productAllocation/trigger/index.vue";
import alnomalRestock from "@/views/productAllocation/alnomalRestock/index.vue";
export default {
  name: "TriggerItem",
  dicts: [
    "sys_product_allocation_status",
    "sys_order_allocation",
    "sys_order_isnew",
    "sys_order_orderstatus",
  ],
  components: { trigger, alnomalRestock },
  data() {
    return {
      openForAlnomal: false,
      row: {},
      openInfo: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 采购分配触发ITEM表格数据
      triggerItemList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productGranularity: null,
        orderStatus: null,
        purchaseOrderNumber: null,
        orderType: null,
        brand: null,
        warehousedQty: null,
        allocatedQty: null,
        unassignedQty: null,
        orderQty: null,
        supplierName: null,
        purchaseAmount: null,
        inTransitQuantity: null,
        differenceCount: null,
        sku: null,
        skc: null,
        spu: null,
        item: null,
        isNew: null,
        contractNumber: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 打开加载层
    openLoading() {
      this.$modal.loading("正在加载，请稍候！");
    },
    handlebyids() {
      if (this.ids.length == 0) {
        this.$modal.msgError("请选择一条数据");
        return;
      }
      this.openLoading();
      byids(this.ids).then((response) => {
        this.$modal.closeLoading();
        this.getList();
      });
    },
    handleSpubyids() {
      if (this.ids.length == 0) {
        this.$modal.msgError("请选择一条数据");
        return;
      }
      this.openLoading();
      triggerSpubyids(this.ids).then((response) => {
        this.$modal.closeLoading();
        this.getList();
      });
    },
    handleItembyids() {
      if (this.ids.length == 0) {
        this.$modal.msgError("请选择一条数据");
        return;
      }
      this.openLoading();
      triggerItembyids(this.ids).then((response) => {
        this.$modal.closeLoading();
        this.getList();
      });
    },
    handleForAlnomal(row) {
      this.row = row;

      this.openForAlnomal = true;
      return;
    },
    getBiCommentSecondDwdList(row) {
      this.row = row;

      this.openInfo = true;
      return;
    },
    /** 查询采购分配触发ITEM列表 */
    getList() {
      this.loading = true;
      listTriggerItem(this.queryParams).then((response) => {
        this.triggerItemList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        productGranularity: null,
        orderStatus: null,
        purchaseOrderNumber: null,
        orderType: null,
        brand: null,
        warehousedQty: null,
        allocatedQty: null,
        unassignedQty: null,
        orderQty: null,
        supplierName: null,
        purchaseAmount: null,
        inTransitQuantity: null,
        differenceCount: null,
        sku: null,
        skc: null,
        spu: null,
        item: null,
        isNew: null,
        contractNumber: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.queryParams.pageSize = 10;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams = {};
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加采购分配触发ITEM";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getTriggerItem(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改采购分配触发ITEM";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateTriggerItem(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTriggerItem(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除采购分配触发ITEM编号为"' + ids + '"的数据项？')
        .then(function () {
          return delTriggerItem(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "productAllocation/triggerItem/export",
        {
          ...this.queryParams,
        },
        `triggerItem_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
